from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from imagekit.models import ImageModel
from tagging.models import GenericTag

class Photo(ImageModel):
    original_image = models.ImageField(upload_to='photos')
    num_views = models.PositiveIntegerField(editable=False, default=0)
    
    class IKOptions:
        spec_module = 'lists.specs'
        image_field = 'original_image'
        save_count_as = 'num_views'

class PhotoItem(Photo):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    
class Item(models.Model):
    name = models.CharField(max_length=140)
    content = models.TextField()
    photos = generic.GenericRelation(PhotoItem)
    
    def __unicode__(self):
        return self.name

class Place(Item):
    address = models.CharField(max_length=140)
    zip = models.CharField(max_length=50)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    
class Restaurant(Place):
    pass

class NumberedItem(Item):
    
    def __unicode__(self):
        return self.name

class List(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    title = models.CharField(max_length=140)
    items = models.ManyToManyField(Item, through='ListMembership')
    ordering = models.CommaSeparatedIntegerField(max_length=100, default="")
    photos = generic.GenericRelation(PhotoItem)
    tags = generic.GenericRelation(GenericTag)
    
    def __unicode__(self):
        return self.title

class ListMembership(models.Model):
    item = models.ForeignKey(Item)
    list = models.ForeignKey(List)
    
    def __unicode__(self):
        return str(self.item) + " in " + str(self.list)
    
class OList(List):
    pass